//
//  SCUpdateMessageAlertView.m
//  SCFoundationKit
//
//  Created by Chao Sheng on 2023/10/31.
//

#import "SCUpdateMessageAlert.h"
#import "SCBasicKit.h"

@implementation SCUpdateMessageAlert

+ (void)showAlertAppVersionType:(SCAppVersionType)type
                       Meassage:(NSString *)message
                        OpenURL:(NSString*)opneUrl {
    
    dispatch_async(dispatch_get_main_queue(), ^{
        UIView *customView = [[UIView alloc] init];
        NSArray *buttons = @[].copy;
        NSString *title = @"温馨提醒";
        
        UIScrollView *scrollView = [[UIScrollView alloc] init];
        [customView addSubview:scrollView];
        [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.right.bottom.equalTo(@0);
            make.width.equalTo(@(sc_w(300)));
            make.height.equalTo(@(sc_w(100)));
        }];
        UILabel *textLabel = [[UILabel alloc] init];
        textLabel.font = sc_PingFangRegularFont(13);
        textLabel.textColor = sc_blackColor;
        textLabel.numberOfLines = 0;
        textLabel.text = message;
        [scrollView addSubview:textLabel];
        [textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.equalTo(@10);
            make.right.bottom.equalTo(@-10);
            make.width.equalTo(@(sc_w(280)));
        }];
        [textLabel layoutIfNeeded];
        if(textLabel.sc_height + 20 > sc_w(100)) {
            CGFloat height = (textLabel.sc_height + 20) < sc_w(200) ? (textLabel.sc_height + 20) :sc_w(200);
            [scrollView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.equalTo(@(height));
            }];
        }
        
        switch (type) {
            case SCAppVersionTypeUpdate:
                buttons = @[@"取消",@"去更新"];
                title = @"检测到最新版本!";
                break;
            case SCAppVersionTypeForceUpdate:
                buttons = @[@"去更新"];
                title = @"检测到最新版本!";
                break;
            case SCAppVersionTypeMessage:
                buttons = @[@"知道了"];
                title = @"系统消息";
                break;
            case SCAppVersionTypeMessageWithUnUsed:
                title = @"系统维护提醒";
                break;
            default:
                break;
        }
        SCAlertView *alert = [SCAlertView new];
        alert.btnClickHidden = false;
        [alert showAlertTitle:title CustomView:customView Buttons:buttons ClickCallBack:^(NSInteger index) {
            if (type == SCAppVersionTypeUpdate) {
                if (index == 1) {
                    //跳转到应用商店,并隐藏弹窗
                    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:opneUrl] options:@{} completionHandler:nil];
                }
                
                [alert hide];

            } else if (type == SCAppVersionTypeForceUpdate) {
                //跳转到应用商店,不隐藏弹窗
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:opneUrl] options:@{} completionHandler:nil];
                
            } else {
                
                [alert hide];
            }
        }];
    });
}

@end
